package xuhaijia.lanou3g.autohome.myself;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.app.Dialog;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;

/**
 * Created by 7 on 16/8/29.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private TextView accountEnter;
    private TextView passwordEnter;
    private Button enterBtn;
    private Dialog mLoginingDlg;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        TextView login = bindView(R.id.activity_login_login);
        enterBtn = bindView(R.id.activity_login_enter);
        LinearLayout otherEnter = bindView(R.id.activty_login_twitter_login);
        TextView forgetKeyword = bindView(R.id.activity_login_forgetkeyword);
        accountEnter = bindView(R.id.activity_login_account);
        passwordEnter = bindView(R.id.activity_login_password);
        login.setOnClickListener(this);
        enterBtn.setOnClickListener(this);
        otherEnter.setOnClickListener(this);
        forgetKeyword.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        initLoginingDlg();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_login_login:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_login_enter:
                showLoginingDlg();
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername(accountEnter.getText().toString());
                bmobUser.setPassword(passwordEnter.getText().toString());
                bmobUser.login(new SaveListener<Object>() {
                    @Override
                    public void done(Object o, BmobException e) {
                        if (e == null) {
                            closeLoginingDlg();
                            finish();

                        } else {
                            Toast.makeText(LoginActivity.this, "请输入正确的账号密码", Toast.LENGTH_SHORT).show();
                            closeLoginingDlg();
                        }
                    }
                });


                break;
        }
    }

    /* 初始化正在登录对话框 */
    private void initLoginingDlg() {

        mLoginingDlg = new Dialog(this, R.style.loginingDlg);
        mLoginingDlg.setContentView(R.layout.logining_dlg);

    }
    private void showLoginingDlg() {
        if (mLoginingDlg != null)
            mLoginingDlg.show();
    }

    /* 关闭正在登录对话框 */
    private void closeLoginingDlg() {
        if (mLoginingDlg != null && mLoginingDlg.isShowing())
            mLoginingDlg.dismiss();
    }

}
